#include "FileOperator.h"

FileOperator::FileOperator(QObject *parent) : QObject(parent)
{

}

bool FileOperator::creatDir(QString Path, QString DirName)
{
    QDir *folder = new QDir();
    QString CurrentPath = Path +"/" + DirName;
    bool exist = folder->exists(CurrentPath);
    if(exist){
        return true;
    }
    else
    {
        bool result = folder->mkdir(CurrentPath);
        return result;
    }
}

bool FileOperator::creatFile(QString Path, QString DirName, QString format)
{
    QString filePath = Path + "/" + DirName + format;
    QFile file(filePath);
    if(file.exists())
    {
        return true;
    }else
    {
       return false;
    }
}

bool FileOperator::writeFile(QString filePath, QString content)
{
    QFile file(filePath);
    QByteArray str = content.toUtf8();
    if(file.exists())
    {
        file.write(str);
    }else
    {
        if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
            return false;
        }
    }
    file.close();
    return true;
}

bool FileOperator::getDirIsExist(QString Path)
{
    QDir folder(Path);
    return folder.exists();
}

bool FileOperator::getFileIsExist(QString fileName)
{
    QFile file(fileName);
    return file.exists();
}

FileOperator * FileOperator::instance = nullptr;

FileOperator *FileOperator::GetInstance()
{
    if(nullptr == instance)
    {
        instance = new FileOperator();
    }
    return instance;
}
